package com.neelsys.service;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neelsys.domain.ui.JSONGraph;
import com.neelsys.domain.visualization.AwardedCertificateEnum;
import com.neelsys.domain.visualization.CourseDesignator;
import com.neelsys.domain.visualization.CourseLevelEnum;
import com.neelsys.domain.visualization.CourseStreamEnum;
import com.neelsys.domain.visualization.EntranceTest;
import com.neelsys.domain.visualization.EntranceTestEnum;
import com.neelsys.domain.visualization.MajorCombination;
import com.neelsys.domain.visualization.MajorsEnum;
import com.neelsys.repository.visualization.MajorCombinationRepository;
import com.neelsys.service.adapter.BlankResultAdapter;
import com.neelsys.service.adapter.MajorCombAdapter;

/**
 * @author mh
 * @since 04.03.11
 */
@Service
public class VisualisationService {

	private final static Logger log = LoggerFactory
			.getLogger(VisualisationService.class);

	@Autowired
	MajorCombinationRepository majorCombRepo;

	@Autowired
	Neo4jOperations template;

	@Transactional
	public JSONGraph persistCourseDesig() {
		CourseDesignator courseDesigntor = new CourseDesignator();
		courseDesigntor.setId("1");
		courseDesigntor.setCourseLevel(CourseLevelEnum.INTERMEDIATE);
		courseDesigntor.setCourseStream(CourseStreamEnum.TECHNICAL);
		template.save(courseDesigntor);

		EntranceTest entranceTest = new EntranceTest();
		entranceTest.setId("1");
		entranceTest.setEntranceTestDetails(EntranceTestEnum.CET);

		CourseDesignator courseDesignator1 = new CourseDesignator();
		courseDesignator1.setId("2");
		courseDesignator1.setCourseLevel(CourseLevelEnum.GRADUATE);
		courseDesignator1.setCourseStream(CourseStreamEnum.TECHNICAL);
		template.save(courseDesignator1);

		MajorCombination majorCombination1 = new MajorCombination();
		majorCombination1.setId("2");
		majorCombination1.setAwardedCertification(AwardedCertificateEnum.BE);
		majorCombination1.setMajorDetails(MajorsEnum.ECE);
		majorCombination1.setMajorDescription("Bachelor Of Engineering");
		majorCombination1.setMinDuration("4");
		majorCombination1.setCourseDesigntator(courseDesignator1);
		template.save(majorCombination1);

		Set<CourseDesignator> et1Course = new HashSet<CourseDesignator>();
		et1Course.add(courseDesignator1);
		entranceTest.setNextCourses(et1Course);

		template.save(entranceTest);

		EntranceTest entranceTest1 = new EntranceTest();
		entranceTest1.setId("2");
		entranceTest1.setEntranceTestDetails(EntranceTestEnum.IITJEE);
		template.save(entranceTest1);

		MajorCombination majorComb = new MajorCombination();
		majorComb.setId("1");
		majorComb.setAwardedCertification(AwardedCertificateEnum.PUC);
		majorComb.setMajorDetails(MajorsEnum.MPC);
		majorComb.setMajorDescription("Pre Univ Major");
		majorComb.setMinDuration("2");
		majorComb.setCourseDesigntator(courseDesigntor);

		Set<EntranceTest> entranceTestSets = new LinkedHashSet<EntranceTest>();
		entranceTestSets.add(entranceTest);
		entranceTestSets.add(entranceTest1);

		majorComb.setEntranceTests(entranceTestSets);

		CourseDesignator courseDesignator2 = new CourseDesignator();
		courseDesignator2.setId("3");
		courseDesignator2.setCourseLevel(CourseLevelEnum.GRADUATE);
		courseDesignator2.setCourseStream(CourseStreamEnum.SCIENCES);
		template.save(courseDesignator2);

		MajorCombination majorCombination2 = new MajorCombination();
		majorCombination2.setId("2");
		majorCombination2.setAwardedCertification(AwardedCertificateEnum.BSc);
		majorCombination2.setMajorDetails(MajorsEnum.MPC);
		majorCombination2.setMajorDescription("Bachelor Of Science");
		majorCombination2.setMinDuration("3");
		majorCombination2.setCourseDesigntator(courseDesignator2);
		template.save(majorCombination2);

		Set<CourseDesignator> et2Course = new HashSet<CourseDesignator>();
		et2Course.add(courseDesignator2);
		majorComb.setNextCourses(et2Course);

		template.save(majorComb);

		return new JSONGraph();

	}

	public JSONGraph getCollege() {
		System.out.println("Get College Called.");
		MajorCombination retrievedComb = majorCombRepo.findById("1");

		MajorCombAdapter adapter=new MajorCombAdapter(template, retrievedComb);
		
		JSONGraph jsonGraph=adapter.adaptToJsonGraph();
		
		return jsonGraph;
	}

	public JSONGraph getDummyJsonGraph() {

		BlankResultAdapter blankResultAdapter = new BlankResultAdapter();

		JSONGraph jsonObject = blankResultAdapter.prepareJsonGraph();

		return jsonObject;

	}

}
